<template>
	<view>
		<view class="page page-bottom-bar page-address">
			<view v-if="addressList.length > 0">
				<!-- 默认地址 -->
				<view class="address_box" v-if="addressobj.name">
					<view class="part1 flex-row-between">
						<view class="info font14">
							<view class="p1 flex-row-start">
								<view>{{addressobj.name}}</view>
								<view class="phone">{{addressobj.mobile}}</view>
								<view class="tag">默认</view>
							</view>
							<view class="p2">{{addressobj.province}}{{addressobj.city}}{{addressobj.area}}{{addressobj.address}}</view>
						</view>
					</view>
				</view>
				<view class="no_data" v-else>暂无默认地址</view>
				<view class="hr7"></view>
				<!-- 列表 -->
				<block v-for="(item,index) in addressList" :key="index">
					<view class="address_box" @click="Onchoose(item)">
						<view class="part1 flex-row-between">
							<view class="info font14">
								<view class="p1 flex-row-start">
									<view>{{item.name}}</view>
									<view class="phone">{{item.mobile}}</view>
								</view>
								<view class="p2">{{item.province}}{{item.city}}{{item.area}}{{item.address}}</view>
							</view>
							<image class="icon-edit" src="https://static.newbeaconhotels.com/app/hotel/my/edit.png" @click.stop="goNext('/pages/my/addressEdit?id=' + item.id)"></image>
						</view>
						<view class="part2 flex-row-between">
							<view class="left flex-row-center" @click.stop="Onset(item.id)">
								<image class="icon" v-if="item.status == 'normal'" src="https://static.newbeaconhotels.com/app/hotel/my/mor_b.png"></image>
								<image class="icon" v-else src="https://static.newbeaconhotels.com/app/hotel/my/mor_a.png"></image>
								<view class="font12">设为默认</view>
							</view>
							<view class="right font12 font-link" @click="Ondelete(item.id)">删除</view>
						</view>
					</view>
				</block>
			</view>
			<view v-else class="no_data">您还没有地址哦~</view>
		</view>
		<view class="fix-bottom-btn flex-row-center">
			<view class="btn flex-row-center font-ffffff font15" @click="goNext('/pages/my/addressEdit')">+ 添 加 地 址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [], // 地址列表
				addressobj: {}, // 默认地址
				isChoose: false,
			}
		},
		onLoad(options) {
			if (options.mode == 'choose') {
				this.isChoose = true
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			// 地址列表
			getList() {
				this.addressobj = {}
				this.$Http({
					url: 'address',
					token: true,
				}).then(res => {
					if (res.code == 1) {
						this.addressList = res.data.data
						res.data.data.forEach(item => {
							if (item.status == 'normal') {
								this.addressobj = item
							}
						})
					}
				});
			},
			// 设置默认地址
			Onset(id) {
				this.$Http({
					url: 'setdefAddress',
					token: true,
					load: true,
					data: { id: id },
				}).then(res => {
					if (res.code == 1) {
						this.getList()
					}
				});
			},
			// 删除地址
			Ondelete(id) {
				let that = this
				uni.showModal({
					title: '操作提示',
					confirmText: '确认',
					confirmColor: '#03004c',
					cancelText: '取消',
					content: '您确认要删除此地址吗?',
					success: function(res) {
						if (res.confirm) {
							that.$Http({
								url: 'delAddress',
								token: true,
								data: { id: id },
							}).then(res => {
								if (res.code == 1) {
									that.getList()
								}
							});
						}
					}
				});
			},
			// 选择地址
			Onchoose(item) {
				if (this.isChoose) {
					uni.$emit('chooseAddr', item)
					// 关闭当前页面
					uni.navigateBack({ delta: 1 });
				}
			},
			// 路由跳转: tag默认false,tag=true跳到tabBar页面
			goNext(url, tag) {
				if (tag) {
					uni.switchTab({ url: url });
				} else {
					uni.navigateTo({ url: url });
				}
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
	@import url("form.less");
</style>
